package cn.zjutleo.village_server.controller;

import cn.zjutleo.village_server.dto.MessageDto;
import cn.zjutleo.village_server.entity.DispatchRepair;
import cn.zjutleo.village_server.entity.RepairApply;

import cn.zjutleo.village_server.service.DispatchRepairService;
import cn.zjutleo.village_server.service.MessageService;
import cn.zjutleo.village_server.service.RepairApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author: 张坤、Qin Zhenghan
 * @DateTime: 2021/8/31 12:26
 * @Description: 该类用于 TODO
 */
@Api(tags="派单号处理")
@RestController()
public class DispatchRepairController {
    @Autowired
    DispatchRepairService dispatchRepairService;

    @Autowired
    RepairApplyService repairApplyService;

    @Autowired
    MessageService messageService;


    @ApiOperation("加入新的派单")
    @PostMapping("/api/insertNewRepairDispatch")
    String insertNewRepairDispatch(@RequestBody DispatchRepair dispatchRepair){
        dispatchRepair.setDr_status(1);
        dispatchRepair.setDr_time(new Date());
        // 创建派单
        dispatchRepairService.InsertNew(dispatchRepair);
        RepairApply repairApply = repairApplyService.selectById(dispatchRepair.getDr_ra_id());
        repairApplyService.updateRaState(dispatchRepair.getDr_ra_id(),1);
        // 给报修者发送通知
        MessageDto messageToApply = new MessageDto();
        messageToApply.setReceiveId(repairApply.getRa_user_id());
        messageToApply.setTitle("维修通知");
        messageToApply.setContent("您的报修订单已被受理，请耐心等待师傅上门处理");
        messageService.sendSystemMessage(messageToApply);
        // 给维修者发送通知
        MessageDto messageToRepair = new MessageDto();
        messageToRepair.setReceiveId(dispatchRepair.getDr_user_id());
        messageToRepair.setTitle("有一条新的维修订单");
        messageToRepair.setContent("新的维修订单");
        messageService.sendSystemMessage(messageToRepair);
        return String.valueOf(dispatchRepair.toString());
    }

}
